import React, { Component } from "react";
import "./index.css";

export default class Item extends Component {
  state = { mouse: false };

  handlerMouse = (type) => {
    this.setState({ mouse: type });
  };

  handlerCheck = (id, checked) => {
    this.props.changeTodo(id, checked);
  };

  handlerDelete = (id) => {
    this.props.deleteTodo(id);
  };

  render() {
    const { id, name, done } = this.props;
    const { mouse } = this.state;

    return (
      <li
        style={{ backgroundColor: mouse ? "#ddd" : "" }}
        onMouseEnter={() => {
          this.handlerMouse(true);
        }}
        onMouseLeave={() => {
          this.handlerMouse(false);
        }}
      >
        <label>
          <input
            type="checkbox"
            checked={done}
            onChange={(e) => {
              this.handlerCheck(id, e.target.checked);
            }}
          />
          <span>{name}</span>
        </label>
        <button
          className="btn btn-danger"
          onClick={() => {
            this.handlerDelete(id);
          }}
          style={{ display: mouse ? "block" : "none" }}
        >
          删除
        </button>
      </li>
    );
  }
}
